/*
 * PuzzleMenuView.java
 */

import java.awt.event.WindowEvent;
import java.awt.event.WindowFocusListener;

import org.jdesktop.application.Action;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;
import javax.swing.JDialog;
import javax.swing.JFrame;

import java.awt.Color;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import javax.swing.JColorChooser;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

/**
 * The application's main frame.
 */
public class PuzzleMenuView extends FrameView implements WindowFocusListener {

    public PuzzleMenuView(SingleFrameApplication app) {
        super(app);

        //app.getMainFrame().addWindowFocusListener(this);
        //JFrame mainFrame = PuzzleMenu.getApplication().getMainFrame();
        //mainFrame.addWindowFocusListener(this);

        initComponents();

        //enumerate fonts into combobox
        String[] fontNames = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
        for (String fontName : fontNames) {
            Font f = new Font(fontName, Font.PLAIN, 12);
            if (f.canDisplay('a')) {
                //... Display only fonts that have the alphabetic characters.
                fontNameComboBox.addItem(fontName);

                //set selected to arial if it exists
                if (fontName.equalsIgnoreCase("arial")) {
                    fontNameComboBox.setSelectedItem(fontName);
                }
            }
        }

        textColorChooser = new JColorChooser(Color.black);
        textColorTextField.setBackground(textColorChooser.getColor());
        tileColorChooser = new JColorChooser(Color.white);
        tileColorTextField.setBackground(tileColorChooser.getColor());
        blankColorChooser = new JColorChooser(Color.yellow);
        blankColorTextField.setBackground(blankColorChooser.getColor());

    }

    @Action
    public void showAboutBox() {
        if (aboutBox == null) {
            JFrame mainFrame = PuzzleMenu.getApplication().getMainFrame();
            aboutBox = new AboutBox(mainFrame);
            aboutBox.setLocationRelativeTo(mainFrame);
        }
        PuzzleMenu.getApplication().show(aboutBox);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        dimensionsPanel = new javax.swing.JPanel();
        rowsLabel = new javax.swing.JLabel();
        columnsLabel = new javax.swing.JLabel();
        rowsComboBox = new javax.swing.JComboBox();
        columnsComboBox = new javax.swing.JComboBox();
        tileSizeLabel = new javax.swing.JLabel();
        tileSizeComboBox = new javax.swing.JComboBox();
        colorsPanel = new javax.swing.JPanel();
        tileColorLabel = new javax.swing.JLabel();
        textColorLabel = new javax.swing.JLabel();
        blankColorLabel = new javax.swing.JLabel();
        tileColorButton = new javax.swing.JButton();
        textColorButton = new javax.swing.JButton();
        blankColorButton = new javax.swing.JButton();
        tileColorTextField = new javax.swing.JTextField();
        textColorTextField = new javax.swing.JTextField();
        blankColorTextField = new javax.swing.JTextField();
        fontsPanel = new javax.swing.JPanel();
        fontNameLabel = new javax.swing.JLabel();
        fontNameComboBox = new javax.swing.JComboBox();
        boldFontCheckBox = new javax.swing.JCheckBox();
        italicFontCheckBox = new javax.swing.JCheckBox();
        jSeparator1 = new javax.swing.JSeparator();
        fontSizeLabel = new javax.swing.JLabel();
        fontSizeComboBox = new javax.swing.JComboBox();
        createButton = new javax.swing.JButton();
        menuBar = new javax.swing.JMenuBar();
        javax.swing.JMenu fileMenu = new javax.swing.JMenu();
        openMenuItem = new javax.swing.JMenuItem();
        saveMenuItem = new javax.swing.JMenuItem();
        javax.swing.JMenuItem exitMenuItem = new javax.swing.JMenuItem();
        javax.swing.JMenu helpMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem aboutMenuItem = new javax.swing.JMenuItem();

        mainPanel.setName("mainPanel"); // NOI18N

        jTabbedPane1.setName("jTabbedPane1"); // NOI18N

        dimensionsPanel.setName("dimensionsPanel"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(PuzzleMenu.class).getContext().getResourceMap(PuzzleMenuView.class);
        rowsLabel.setText(resourceMap.getString("rowsLabel.text")); // NOI18N
        rowsLabel.setName("rowsLabel"); // NOI18N

        columnsLabel.setText(resourceMap.getString("columnsLabel.text")); // NOI18N
        columnsLabel.setName("columnsLabel"); // NOI18N

        rowsComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" }));
        rowsComboBox.setSelectedIndex(3);
        rowsComboBox.setName("rowsComboBox"); // NOI18N

        columnsComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" }));
        columnsComboBox.setSelectedIndex(3);
        columnsComboBox.setName("columnsComboBox"); // NOI18N

        tileSizeLabel.setText(resourceMap.getString("tileSizeLabel.text")); // NOI18N
        tileSizeLabel.setName("tileSizeLabel"); // NOI18N

        tileSizeComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "50", "60", "70", "80", "90", "100" }));
        tileSizeComboBox.setSelectedIndex(1);
        tileSizeComboBox.setName("tileSizeComboBox"); // NOI18N

        javax.swing.GroupLayout dimensionsPanelLayout = new javax.swing.GroupLayout(dimensionsPanel);
        dimensionsPanel.setLayout(dimensionsPanelLayout);
        dimensionsPanelLayout.setHorizontalGroup(
            dimensionsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(dimensionsPanelLayout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addGroup(dimensionsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(tileSizeLabel)
                    .addComponent(columnsLabel)
                    .addComponent(rowsLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(dimensionsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(tileSizeComboBox, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(rowsComboBox, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(columnsComboBox, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(280, Short.MAX_VALUE))
        );
        dimensionsPanelLayout.setVerticalGroup(
            dimensionsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(dimensionsPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(dimensionsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(rowsLabel)
                    .addComponent(rowsComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(dimensionsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(columnsLabel)
                    .addComponent(columnsComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(dimensionsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tileSizeLabel)
                    .addComponent(tileSizeComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(54, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab(resourceMap.getString("dimensionsPanel.TabConstraints.tabTitle"), dimensionsPanel); // NOI18N

        colorsPanel.setName("colorsPanel"); // NOI18N

        tileColorLabel.setText(resourceMap.getString("tileColorLabel.text")); // NOI18N
        tileColorLabel.setName("tileColorLabel"); // NOI18N

        textColorLabel.setText(resourceMap.getString("textColorLabel.text")); // NOI18N
        textColorLabel.setName("textColorLabel"); // NOI18N

        blankColorLabel.setText(resourceMap.getString("blankColorLabel.text")); // NOI18N
        blankColorLabel.setName("blankColorLabel"); // NOI18N

        tileColorButton.setText(resourceMap.getString("tileColorButton.text")); // NOI18N
        tileColorButton.setName("tileColorButton"); // NOI18N
        tileColorButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tileColorButtonActionPerformed(evt);
            }
        });

        textColorButton.setText(resourceMap.getString("textColorButton.text")); // NOI18N
        textColorButton.setName("textColorButton"); // NOI18N
        textColorButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                textColorButtonActionPerformed(evt);
            }
        });

        blankColorButton.setText(resourceMap.getString("blankColorButton.text")); // NOI18N
        blankColorButton.setName("blankColorButton"); // NOI18N
        blankColorButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                blankColorButtonActionPerformed(evt);
            }
        });

        tileColorTextField.setEditable(false);
        tileColorTextField.setText(resourceMap.getString("tileColorTextField.text")); // NOI18N
        tileColorTextField.setName("tileColorTextField"); // NOI18N

        textColorTextField.setEditable(false);
        textColorTextField.setText(resourceMap.getString("textColorTextField.text")); // NOI18N
        textColorTextField.setName("textColorTextField"); // NOI18N

        blankColorTextField.setEditable(false);
        blankColorTextField.setText(resourceMap.getString("blankColorTextField.text")); // NOI18N
        blankColorTextField.setName("blankColorTextField"); // NOI18N

        javax.swing.GroupLayout colorsPanelLayout = new javax.swing.GroupLayout(colorsPanel);
        colorsPanel.setLayout(colorsPanelLayout);
        colorsPanelLayout.setHorizontalGroup(
            colorsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(colorsPanelLayout.createSequentialGroup()
                .addGap(25, 25, 25)
                .addGroup(colorsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(colorsPanelLayout.createSequentialGroup()
                        .addGroup(colorsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(textColorLabel)
                            .addComponent(tileColorLabel))
                        .addGap(7, 7, 7))
                    .addGroup(colorsPanelLayout.createSequentialGroup()
                        .addComponent(blankColorLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                .addGroup(colorsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(colorsPanelLayout.createSequentialGroup()
                        .addComponent(textColorButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(textColorTextField))
                    .addGroup(colorsPanelLayout.createSequentialGroup()
                        .addComponent(tileColorButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(tileColorTextField))
                    .addGroup(colorsPanelLayout.createSequentialGroup()
                        .addComponent(blankColorButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(blankColorTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(161, Short.MAX_VALUE))
        );
        colorsPanelLayout.setVerticalGroup(
            colorsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(colorsPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(colorsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tileColorLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tileColorButton)
                    .addComponent(tileColorTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(colorsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(textColorLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(textColorButton)
                    .addComponent(textColorTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(colorsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(blankColorButton)
                    .addComponent(blankColorLabel)
                    .addComponent(blankColorTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(45, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab(resourceMap.getString("colorsPanel.TabConstraints.tabTitle"), colorsPanel); // NOI18N

        fontsPanel.setName("fontsPanel"); // NOI18N

        fontNameLabel.setText(resourceMap.getString("fontNameLabel.text")); // NOI18N
        fontNameLabel.setName("fontNameLabel"); // NOI18N

        fontNameComboBox.setName("fontNameComboBox"); // NOI18N

        boldFontCheckBox.setText(resourceMap.getString("boldFontCheckBox.text")); // NOI18N
        boldFontCheckBox.setName("boldFontCheckBox"); // NOI18N

        italicFontCheckBox.setText(resourceMap.getString("italicFontCheckBox.text")); // NOI18N
        italicFontCheckBox.setName("italicFontCheckBox"); // NOI18N

        jSeparator1.setName("jSeparator1"); // NOI18N

        fontSizeLabel.setText(resourceMap.getString("fontSizeLabel.text")); // NOI18N
        fontSizeLabel.setName("fontSizeLabel"); // NOI18N

        fontSizeComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "8", "10", "12", "14", "16", "18", "20" }));
        fontSizeComboBox.setSelectedIndex(2);
        fontSizeComboBox.setName("fontSizeComboBox"); // NOI18N

        javax.swing.GroupLayout fontsPanelLayout = new javax.swing.GroupLayout(fontsPanel);
        fontsPanel.setLayout(fontsPanelLayout);
        fontsPanelLayout.setHorizontalGroup(
            fontsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(fontsPanelLayout.createSequentialGroup()
                .addGroup(fontsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(fontsPanelLayout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(fontsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(italicFontCheckBox)
                            .addComponent(boldFontCheckBox)
                            .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 367, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, fontsPanelLayout.createSequentialGroup()
                        .addGap(26, 26, 26)
                        .addGroup(fontsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(fontSizeLabel)
                            .addComponent(fontNameLabel))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(fontsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(fontNameComboBox, 0, 291, Short.MAX_VALUE)
                            .addComponent(fontSizeComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(18, Short.MAX_VALUE))
        );
        fontsPanelLayout.setVerticalGroup(
            fontsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(fontsPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(fontsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(fontNameLabel)
                    .addComponent(fontNameComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(fontsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(fontSizeLabel)
                    .addComponent(fontSizeComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(1, 1, 1)
                .addComponent(boldFontCheckBox)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(italicFontCheckBox)
                .addGap(44, 44, 44))
        );

        jTabbedPane1.addTab(resourceMap.getString("fontsPanel.TabConstraints.tabTitle"), fontsPanel); // NOI18N

        createButton.setText(resourceMap.getString("createButton.text")); // NOI18N
        createButton.setName("createButton"); // NOI18N
        createButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                createButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addGap(167, 167, 167)
                .addComponent(createButton)
                .addContainerGap(168, Short.MAX_VALUE))
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 165, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(createButton)
                .addContainerGap())
        );

        menuBar.setName("menuBar"); // NOI18N

        fileMenu.setText(resourceMap.getString("fileMenu.text")); // NOI18N
        fileMenu.setName("fileMenu"); // NOI18N

        openMenuItem.setText(resourceMap.getString("openMenuItem.text")); // NOI18N
        openMenuItem.setName("openMenuItem"); // NOI18N
        openMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                openMenuItemActionPerformed(evt);
            }
        });
        fileMenu.add(openMenuItem);

        saveMenuItem.setText(resourceMap.getString("saveMenuItem.text")); // NOI18N
        saveMenuItem.setName("saveMenuItem"); // NOI18N
        saveMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveMenuItemActionPerformed(evt);
            }
        });
        fileMenu.add(saveMenuItem);

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(PuzzleMenu.class).getContext().getActionMap(PuzzleMenuView.class, this);
        exitMenuItem.setAction(actionMap.get("quit")); // NOI18N
        exitMenuItem.setName("exitMenuItem"); // NOI18N
        fileMenu.add(exitMenuItem);

        menuBar.add(fileMenu);

        helpMenu.setText(resourceMap.getString("helpMenu.text")); // NOI18N
        helpMenu.setName("helpMenu"); // NOI18N

        aboutMenuItem.setAction(actionMap.get("showAboutBox")); // NOI18N
        aboutMenuItem.setName("aboutMenuItem"); // NOI18N
        helpMenu.add(aboutMenuItem);

        menuBar.add(helpMenu);

        setComponent(mainPanel);
        setMenuBar(menuBar);
    }// </editor-fold>//GEN-END:initComponents

private void createButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_createButtonActionPerformed
    TilePuzzle puzzle = new TilePuzzle();

    //get puzzle title
    JFrame mainFrame = PuzzleMenu.getApplication().getMainFrame();
    String myTitle = (String) JOptionPane.showInputDialog(mainFrame, "Enter Puzzle Title:\n");

    //set puzzle title
    puzzle.setTitle(myTitle);

    //set puzzle dimensions
    int myRows = Integer.parseInt(rowsComboBox.getSelectedItem().toString());
    int myCols = Integer.parseInt(columnsComboBox.getSelectedItem().toString());
    int myTileSize = Integer.parseInt(tileSizeComboBox.getSelectedItem().toString());
    puzzle.setRowsCols(myRows, myCols);
    puzzle.setHeight(myRows * myTileSize + 40);
    puzzle.setWidth(myCols * myTileSize);

    //set puzzle fonts
    int myFontStyle = 0;
    if (boldFontCheckBox.isSelected()) {
        myFontStyle = myFontStyle + Font.BOLD;
    }

    if (italicFontCheckBox.isSelected()) {
        myFontStyle = myFontStyle + Font.ITALIC;
    }

    Font myFont = new Font(fontNameComboBox.getSelectedItem().toString(), myFontStyle, Integer.parseInt(fontSizeComboBox.getSelectedItem().toString()));
    puzzle.setMyFont(myFont);

    //set puzzle colors
    puzzle.setTileColor(tileColorChooser.getColor());
    puzzle.setTextColor(textColorChooser.getColor());
    puzzle.setBlankColor(blankColorChooser.getColor());

    //display puzzle
    puzzle.layIt();
    puzzle.addWindowFocusListener(this);

}//GEN-LAST:event_createButtonActionPerformed

private void tileColorButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tileColorButtonActionPerformed
    JFrame mainFrame = PuzzleMenu.getApplication().getMainFrame();

    if (tileColorChooser == null) {
        tileColorChooser = new JColorChooser(Color.white);
    }

    PuzzleMenu.getApplication().show(JColorChooser.createDialog(mainFrame, "Choose a color...", true, tileColorChooser, null, null));
    tileColorTextField.setBackground(tileColorChooser.getColor());
}//GEN-LAST:event_tileColorButtonActionPerformed

private void textColorButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_textColorButtonActionPerformed
    JFrame mainFrame = PuzzleMenu.getApplication().getMainFrame();

    if (textColorChooser == null) {
        textColorChooser = new JColorChooser(Color.black);
    }

    PuzzleMenu.getApplication().show(JColorChooser.createDialog(mainFrame, "Choose a color...", true, textColorChooser, null, null));
    textColorTextField.setBackground(textColorChooser.getColor());
}//GEN-LAST:event_textColorButtonActionPerformed

private void blankColorButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_blankColorButtonActionPerformed
    JFrame mainFrame = PuzzleMenu.getApplication().getMainFrame();

    if (blankColorChooser == null) {
        blankColorChooser = new JColorChooser(Color.yellow);
    }

    PuzzleMenu.getApplication().show(JColorChooser.createDialog(mainFrame, "Choose a color...", true, blankColorChooser, null, null));
    blankColorTextField.setBackground(blankColorChooser.getColor());
}//GEN-LAST:event_blankColorButtonActionPerformed

private void saveMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveMenuItemActionPerformed

    //check if there is a puzzle to save
    if (topPuzzle != null) {
        final JFileChooser chooser = new JFileChooser(".");
        ExampleFileFilter filter = new ExampleFileFilter();
        filter.addExtension("pzl");
        filter.setDescription("puzzles only");
        chooser.setFileFilter(filter);
        chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);

        String defaultFileName = topPuzzle.getTitle();
        File dataDirectory = new File(".");
        File defaultFile = new File(dataDirectory, defaultFileName + ".pzl");
        chooser.setSelectedFile(defaultFile);

        if (chooser.showSaveDialog(null) != JFileChooser.APPROVE_OPTION) {
            return;
        }

        try {
            FileOutputStream fos = new FileOutputStream(chooser.getSelectedFile());
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(topPuzzle);
            oos.close();
            topPuzzle.dispose();
        } catch (Exception e) {
        }
    }
}//GEN-LAST:event_saveMenuItemActionPerformed

private void openMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openMenuItemActionPerformed

    final JFileChooser chooser = new JFileChooser(".");
    ExampleFileFilter filter = new ExampleFileFilter();
    filter.addExtension("pzl");
    filter.setDescription("puzzles only");
    chooser.setFileFilter(filter);
    chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);

    if (chooser.showOpenDialog(null) != JFileChooser.APPROVE_OPTION) {
        return;
    }
    
    try {
        FileInputStream fis = new FileInputStream(chooser.getSelectedFile());
        ObjectInputStream ois = new ObjectInputStream(fis);
        TilePuzzle myPuzzle = (TilePuzzle) ois.readObject();
        ois.close();
        myPuzzle.setVisible(true);
    } catch (Exception e) {
    }

}//GEN-LAST:event_openMenuItemActionPerformed

    @Override
    public void windowGainedFocus(WindowEvent e) {
        //System.out.println("WindowListener method called: windowGainedFocus.");
    }

    @Override
    public void windowLostFocus(WindowEvent e) {
        //System.out.println("WindowListener method called: windowLostFocus.");

        topPuzzle = (TilePuzzle) e.getComponent();
        //System.out.println(topPuzzle.getTitle());
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton blankColorButton;
    private javax.swing.JLabel blankColorLabel;
    private javax.swing.JTextField blankColorTextField;
    private javax.swing.JCheckBox boldFontCheckBox;
    private javax.swing.JPanel colorsPanel;
    private javax.swing.JComboBox columnsComboBox;
    private javax.swing.JLabel columnsLabel;
    private javax.swing.JButton createButton;
    private javax.swing.JPanel dimensionsPanel;
    private javax.swing.JComboBox fontNameComboBox;
    private javax.swing.JLabel fontNameLabel;
    private javax.swing.JComboBox fontSizeComboBox;
    private javax.swing.JLabel fontSizeLabel;
    private javax.swing.JPanel fontsPanel;
    private javax.swing.JCheckBox italicFontCheckBox;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JMenuItem openMenuItem;
    private javax.swing.JComboBox rowsComboBox;
    private javax.swing.JLabel rowsLabel;
    private javax.swing.JMenuItem saveMenuItem;
    private javax.swing.JButton textColorButton;
    private javax.swing.JLabel textColorLabel;
    private javax.swing.JTextField textColorTextField;
    private javax.swing.JButton tileColorButton;
    private javax.swing.JLabel tileColorLabel;
    private javax.swing.JTextField tileColorTextField;
    private javax.swing.JComboBox tileSizeComboBox;
    private javax.swing.JLabel tileSizeLabel;
    // End of variables declaration//GEN-END:variables
    
    private JDialog aboutBox;
    private JColorChooser textColorChooser;
    private JColorChooser tileColorChooser;
    private JColorChooser blankColorChooser;
    private TilePuzzle topPuzzle;
}
